package com.nation.controller;

import com.nation.domain.User;
import com.nation.domain.VOverseasChinese;
import com.nation.domain.VPoliticsAndMilitary;
import com.nation.service.PoliticService;
import com.nation.utils.ReadAndStoreFile;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dqd on 2017/11/3.
 */
@Controller
@RequestMapping(value = "/Politics")
public class PoliticController {
    @Autowired
    PoliticService PoliticService;

    /**
     * 政治军事
     * @param map
     * @return
     */
    @RequestMapping(value = "/politics")
    public  String politic(ModelMap map){
        List<VPoliticsAndMilitary> plist=PoliticService.findAllPolitic();
        map.put("plist",plist);
        return "Politics/politic";
    }

    /**
     * 修改页面
     * @param country_code
     * @param map
     * @return
     */
    @RequestMapping(value = "/politic_update")
    public String politic_update(String country_code,ModelMap map){
        VPoliticsAndMilitary politic=PoliticService.findByCountryCode(country_code);
        map.put("politic",politic);
        return "Politics/politic-update";
    }

    /**
     * 修改信息
     * @param VPoliticsAndMilitary
     * @return
     */
    @RequestMapping(value = "/politic_update.do")
    @ResponseBody
    public String politic_update_do(VPoliticsAndMilitary VPoliticsAndMilitary){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VPoliticsAndMilitary.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=PoliticService.updatePolitic(VPoliticsAndMilitary);
        if(judge)
            return "修改成功";
        else
            return "修改失败";
    }
    @RequestMapping(value = "/politic_add")
    public String politic_add(){
        return "Politics/politic-add";
    }
    @RequestMapping(value="/politic_add.do")
    @ResponseBody
//    var parameter={statistics,conferences,leader,office_time,constitution,government,government_form，government_form,
// parliament:parliament,country_code,judiciary,reference ,political_angency,political_persons,parties,political_changes,other_information,military};
    public String politic_add_do(HttpServletRequest request,String statistics,String conferences,String leader,String office_time,String constitution,String government,String government_form,
                                 String parliament,String country_code,String judiciary,String reference ,String political_angency,String political_persons,
                                 String parties,String political_changes,String other_information,String military,
                                 @RequestParam(value = "picture",required = false)MultipartFile[] pictures){

        VPoliticsAndMilitary VPoliticsAndMilitary=new VPoliticsAndMilitary(statistics,conferences,leader,office_time,constitution,government,government_form,
                                                                           parliament,country_code,judiciary,reference ,political_angency,political_persons,parties,political_changes,other_information,military);
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        VPoliticsAndMilitary.setRecoder_id(user.getYHZH());
        VPoliticsAndMilitary.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=null;
        try{
            if (pictures!=null&&pictures.length>0) {//有文件上传
                String path=request.getSession().getServletContext().getResource("/").getPath();
                path += ReadAndStoreFile.POLITIC;
                System.out.println("path--"+path);

                String picture_path="";
//          将MultipartFile格式转换成File
                for (int i=0;i<pictures.length;i++){
                    String temp=ReadAndStoreFile.save(pictures[i],path);
                    temp=temp.substring(temp.indexOf("nation/"+ReadAndStoreFile.POLITIC));
                    if (i == (pictures.length-1) || pictures.length == 1){
                        picture_path=temp+picture_path;
                    }else{
                        picture_path+=temp+",";
                    }
                }
                VPoliticsAndMilitary.setPicture(picture_path);
            }

            judge=PoliticService.insert(VPoliticsAndMilitary);
        }catch (Exception e){
            e.printStackTrace();
            return "添加失败";
        }
        if(judge)
            return "添加成功";
        else
            return "添加失败";
    }
    @RequestMapping(value = "/InfoSearch")
    public String InfoSearch(HttpServletRequest request, ModelMap mmap){
        String recoder_id=request.getParameter("recoder_id");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("recoder_id",recoder_id);
        List<VPoliticsAndMilitary> elist=PoliticService.findByCondition(map);
        mmap.put("plist",elist);
        return "Politics/politic";
    }
    @RequestMapping(value = "/politic_delete")
    @ResponseBody
    public String baseinfo_delete(String country_code){
        Boolean judge=null;
        try{
            judge=PoliticService.delete(country_code);
        }catch (Exception e){
            e.printStackTrace();
            return "删除失败";
        }
        if(judge)
            return "删除成功";
        else
            return "删除失败";
    }
}
